﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyCongDoanVien.UserControls
{
    public partial class ucXoaCongDoanVien : UserControl
    {
        public ucXoaCongDoanVien()
        {
            InitializeComponent();
        }

        private void txtNhapTenCDV_TextChanged(object sender, EventArgs e)
        {
            if (txtNhapTenCDV.Text.Trim() != "" || cboChonToCongDoan.Text.Trim() != "")
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }

        private void cboChonToCongDoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtNhapTenCDV.Text.Trim() != "" || cboChonToCongDoan.Text.Trim() != "")
            {
                btnDongY.Enabled = true;
            }
            else
            {
                btnDongY.Enabled = false;
            }
        }

        private void ucXoaCongDoanVien_Load(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.Name == "txtNhapTenCDV" || ctrl.Name == "cboChonToCongDoan" || (ctrl.Name.Contains("lbl") == true)
                    || ctrl.Name == "btnThoat")
                {
                    ctrl.Enabled = true;
                }
                else
                {
                    ctrl.Enabled = false;
                }
            }
            cboChonToCongDoan.DataSource = ToCongDoanBUS.LayDanhSach();
            cboChonToCongDoan.DisplayMember = "TenToCongDoan";
            cboChonToCongDoan.SelectedIndex = -1;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show("Bạn chắc chắn muốn xóa công đoàn viên này?", "Thông báo", MessageBoxButtons.YesNo);

            if (dlgResult == DialogResult.Yes)
            {
                try
                {
                    string maCDV = dtgDanhSachCDV.CurrentRow.Cells["MaCDV"].Value.ToString();
                    CongDoanVienBUS.Xoa(maCDV);
                    this.btnDongY_Click(sender, e);
                    MessageBox.Show("Đã xóa\n");
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            dtgDanhSachCDV.Enabled = true;
            btnXoa.Enabled = true;

            string strHoTen = txtNhapTenCDV.Text.Trim();
            ToCongDoanDTO toCDDto = new ToCongDoanDTO();
            if (cboChonToCongDoan.Text.Trim() != "")
            {
                toCDDto = (ToCongDoanDTO)cboChonToCongDoan.SelectedItem;
            }

            try
            {
                dtgDanhSachCDV.DataSource = CongDoanVienBUS.TimKiem("", strHoTen, toCDDto);
                dtgDanhSachCDV.Columns["tocongdoanid"].Visible = false;
                dtgDanhSachCDV.Columns["tocongdoan"].Visible = false;
                dtgDanhSachCDV.Columns["tongiao"].Visible = false;
                dtgDanhSachCDV.Columns["tongiaoid"].Visible = false;
                dtgDanhSachCDV.Columns["dantoc"].Visible = false;
                dtgDanhSachCDV.Columns["dantocid"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
